<?php

include_once("bean/uploadbean.class.php");
class JKUpload 
{

 	
    function JKUpload()
	{
		//store pointer to db onject

	}
	
      
      function crop_image($nw, $nh, $source, $stype, $dest) {
          $size = getimagesize($source);
          $w = $size[0];
          $h = $size[1];
          switch($stype) {
              case 'gif':
              $simg = imagecreatefromgif($source);
              break;
              case 'jpg':
              $simg = imagecreatefromjpeg($source);
              break;
              case 'png':
              $simg = imagecreatefrompng($source);
              break;
          }
          $dimg = imagecreatetruecolor($nw, $nh);
          $wm = $w/$nw;
          $hm = $h/$nh;
          $h_height = $nh/2;
          $w_height = $nw/2;
		  $src_x = $w - $nw;
		  $dst_h = $h - $nh;
          if($w> $h) {
              $adjusted_width = $w / $hm;
              $half_width = $adjusted_width / 2;
              $int_width = $half_width - $w_height;
              imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
          } elseif(($w <$h) || ($w == $h)) {
              $adjusted_height = $h / $wm;
			  
              $half_height = $adjusted_height / 2;
              $int_height = $half_height - $h_height;
              imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
          } else {
              imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
          }
          imagejpeg($dimg,$dest,100);
      }

	function upload_files($image, $photo, $type) {
		$error = -1;
		
		$source = $image;
		if($type == "jpeg" || $type == "jpe" || $type == "jpg") {
			$src = imagecreatefromjpeg($source);
		} else if($type == "gif") {
			$src = imagecreatefromgif($source);
		} else if($type == "png") {
			$src = imagecreatefrompng($source);
		} else if($type == "bmp") {
			$src = uploadbean::imagecreatefrombmp($source);
		}
		list($width,$height) = getimagesize($source);
		if($width >= $height) {
			if($width >= 666) {
				$newwidth = 666;
				$newheight =($height/$width)*666;
			} else {
				$newwidth = $width;
				$newheight = $height;
			}
		} else {
			if($width >= 318) {
				$newwidth = 318;
				$newheight =($height/$width)*318;
			} else {
				$newwidth = $width;
				$newheight = $height;
			}
		}
		$tmp=imagecreatetruecolor($newwidth,$newheight);
		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
		
		$randomname = $photo.date("YmdHms").mt_rand();		
		$$name = md5($randomname).".$type";

		$target = "../".UPLOAD_FILES.$$name;
		
		if($type == "jpeg" || $type == "jpe" || $type == "jpg") {
			imagejpeg($tmp, $target);
		} else if($type == "gif") {
			imagegif($tmp, $target);
		} else if($type == "png") {
			imagepng($tmp, $target);
		} else if($type == "bmp") {
			uploadbean::imagebmp($tmp, $target);
		}
					
		imagedestroy($src);
		imagedestroy($tmp);
		
		return $$name;
	}
	
	function upload_files_txt($image, $photo, $type) {
		if (is_uploaded_file($image)) {
			 $randomname = date("YmdHms").mt_rand();		
			 $name = $photo.md5($randomname).".".$type;
			 $result = move_uploaded_file($image,"../".UPLOAD_DOWNLOAD."$name");
		} 
		return $name;
	}
	
	function upload_flash($image, $photo, $type) {
		if (is_uploaded_file($image)) {
			 $randomname = date("YmdHms").mt_rand();		
			 $name = $photo.md5($randomname).".".$type;
			 $result = move_uploaded_file($image,"../".UPLOAD_FILES."$name");
		} 
		return $name;
	}
	
	function upload_logo($image, $photo, $type) {
		$error = -1;
		
		$source = $image;
		if($type == "jpeg" || $type == "jpe" || $type == "jpg") {
			$src = imagecreatefromjpeg($source);
		} else if($type == "gif") {
			$src = imagecreatefromgif($source);
		} else if($type == "png") {
			$src = imagecreatefrompng($source);
		} else if($type == "bmp") {
			$src = uploadbean::imagecreatefrombmp($source);
		}
		list($width,$height) = getimagesize($source);
		if($width >= $height) {
			if($width >= 666) {
				$newwidth = 666;
				$newheight =($height/$width)*666;
			} else {
				$newwidth = $width;
				$newheight = $height;
			}
		} else {
			if($width >= 318) {
				$newwidth = 318;
				$newheight =($height/$width)*318;
			} else {
				$newwidth = $width;
				$newheight = $height;
			}
		}
		$tmp=imagecreatetruecolor($newwidth,$newheight);
		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
		
		$randomname = $photo.date("YmdHms").mt_rand();		
		$$name = md5($randomname).".$type";

		$target = "../".UPLOAD_DIR_LOGO.$$name;
		
		if($type == "jpeg" || $type == "jpe" || $type == "jpg") {
			imagejpeg($tmp, $target);
		} else if($type == "gif") {
			imagegif($tmp, $target);
		} else if($type == "png") {
			imagepng($tmp, $target);
		} else if($type == "bmp") {
			uploadbean::imagebmp($tmp, $target);
		}
					
		imagedestroy($src);
		imagedestroy($tmp);
		
		return $$name;
	}
	
	function upload_product_thumbnail($image, $photo, $type) {
		$error = -1;
		
		$source = $image;
		if($type == "jpeg" || $type == "jpe" || $type == "jpg") {
			$src = imagecreatefromjpeg($source);
		} else if($type == "gif") {
			$src = imagecreatefromgif($source);
		} else if($type == "png") {
			$src = imagecreatefrompng($source);
		} else if($type == "bmp") {
			$src = uploadbean::imagecreatefrombmp($source);
		}
		list($width,$height) = getimagesize($source);
		if($width >= $height) {
			if($width >= 666) {
				$newwidth = 666;
				$newheight =($height/$width)*666;
			} else {
				$newwidth = $width;
				$newheight = $height;
			}
		} else {
			if($width >= 318) {
				$newwidth = 318;
				$newheight =($height/$width)*318;
			} else {
				$newwidth = $width;
				$newheight = $height;
			}
		}
		$tmp=imagecreatetruecolor($newwidth,$newheight);
		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
		
		$randomname = $photo.date("YmdHms").mt_rand();		
		$$name = md5($randomname).".$type";

		$target = "../".UPLOAD_PRODUCT.$$name;
		$target1 = "../".UPLOAD_PRODUCT_THUMB.$$name;
		
		if($type == "jpeg" || $type == "jpe" || $type == "jpg") {
			imagejpeg($tmp, $target);
		} else if($type == "gif") {
			imagegif($tmp, $target);
		} else if($type == "png") {
			imagepng($tmp, $target);
		} else if($type == "bmp") {
			uploadbean::imagebmp($tmp, $target);
		}
					
		imagedestroy($src);
		imagedestroy($tmp);
		JKUpload::crop_image(SIZETHUMB, SIZETHUMB, $target, $type, $target1);	
		
		return $$name;
	}
	
	function upload_flag_thumbnail($image, $photo, $type) {
		$error = -1;
		
		$source = $image;
		if($type == "jpeg" || $type == "jpe" || $type == "jpg") {
			$src = imagecreatefromjpeg($source);
		} else if($type == "gif") {
			$src = imagecreatefromgif($source);
		} else if($type == "png") {
			$src = imagecreatefrompng($source);
		} else if($type == "bmp") {
			$src = uploadbean::imagecreatefrombmp($source);
		}
		list($width,$height) = getimagesize($source);
		if($width >= $height) {
			if($width >= 666) {
				$newwidth = 666;
				$newheight =($height/$width)*666;
			} else {
				$newwidth = $width;
				$newheight = $height;
			}
		} else {
			if($width >= 318) {
				$newwidth = 318;
				$newheight =($height/$width)*318;
			} else {
				$newwidth = $width;
				$newheight = $height;
			}
		}
		$tmp=imagecreatetruecolor($newwidth,$newheight);
		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
		
		$randomname = $photo.date("YmdHms").mt_rand();		
		$$name = md5($randomname).".$type";

		$target = "../".UPLOAD_FLAG.$$name;
		$target1 = "../".UPLOAD_FLAG_THUMB.$$name;
		
		if($type == "jpeg" || $type == "jpe" || $type == "jpg") {
			imagejpeg($tmp, $target);
		} else if($type == "gif") {
			imagegif($tmp, $target);
		} else if($type == "png") {
			imagepng($tmp, $target);
		} else if($type == "bmp") {
			uploadbean::imagebmp($tmp, $target);
		}
					
		imagedestroy($src);
		imagedestroy($tmp);
		JKUpload::crop_image(WFLAG, HFLAG, $target, $type, $target1);	
		
		return $$name;
	}
	
	function upload_news_thumbnail($image, $photo, $type) {
		$error = -1;
		
		$source = $image;
		if($type == "jpeg" || $type == "jpe" || $type == "jpg") {
			$src = imagecreatefromjpeg($source);
		} else if($type == "gif") {
			$src = imagecreatefromgif($source);
		} else if($type == "png") {
			$src = imagecreatefrompng($source);
		} else if($type == "bmp") {
			$src = uploadbean::imagecreatefrombmp($source);
		}
		list($width,$height) = getimagesize($source);
		if($width >= $height) {
			if($width >= 666) {
				$newwidth = 666;
				$newheight =($height/$width)*666;
			} else {
				$newwidth = $width;
				$newheight = $height;
			}
		} else {
			if($width >= 318) {
				$newwidth = 318;
				$newheight =($height/$width)*318;
			} else {
				$newwidth = $width;
				$newheight = $height;
			}
		}
		$tmp=imagecreatetruecolor($newwidth,$newheight);
		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
		
		$randomname = $photo.date("YmdHms").mt_rand();		
		$$name = md5($randomname).".$type";

		$target = "../".UPLOAD_NEWS.$$name;
		$target1 = "../".UPLOAD_NEWS_THUMB.$$name;
		
		if($type == "jpeg" || $type == "jpe" || $type == "jpg") {
			imagejpeg($tmp, $target);
		} else if($type == "gif") {
			imagegif($tmp, $target);
		} else if($type == "png") {
			imagepng($tmp, $target);
		} else if($type == "bmp") {
			uploadbean::imagebmp($tmp, $target);
		}
					
		imagedestroy($src);
		imagedestroy($tmp);
		JKUpload::crop_image(SIZETHUMB, SIZETHUMB, $target, $type, $target1);	
		
		return $$name;
	}
	
	
	function upload_scale_file($image, $photo, $type,$path) {
		$error = -1;
		
		$source = $image;
		if($type == "jpeg" || $type == "jpe" || $type == "jpg") {
			$src = imagecreatefromjpeg($source);
		} else if($type == "gif") {
			$src = imagecreatefromgif($source);
		} else if($type == "png") {
			$src = imagecreatefrompng($source);
		} else if($type == "bmp") {
			$src = uploadbean::imagecreatefrombmp($source);
		}
		list($width,$height) = getimagesize($source);
		if($width >= $height) {
			if($width >= 400) {
				$newwidth = 400;
				$newheight =($height/$width)*400;
			} else {
				$newwidth = $width;
				$newheight = $height;
			}
		} else {
			if($width >= 200) {
				$newwidth = 200;
				$newheight =($height/$width)*200;
			} else {
				$newwidth = $width;
				$newheight = $height;
			}
		}
		$tmp=imagecreatetruecolor($newwidth,$newheight);
		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
				
		$$name = $photo."_".date("YmdHms").".$type";

		$target = "../".$path.$photo."_".date("YmdHms").".$type";
		
		if($type == "jpeg" || $type == "jpe" || $type == "jpg") {
			imagejpeg($tmp, $target);
		} else if($type == "gif") {
			imagegif($tmp, $target);
		} else if($type == "png") {
			imagepng($tmp, $target);
		} else if($type == "bmp") {
			uploadbean::imagebmp($tmp, $target);
		}
					
		imagedestroy($src);
		imagedestroy($tmp);
		
		return $$name;
	}
	
}
?>